##############################################
# iNVMS-9000 Platform Complile Script 
#
# Building Requires:
# sudo apt-get update
# sudo apt-get install autoconf autotools-dev build-essential libbz2-dev make automake libtool cmake tar
# sudo apt-get install libncurses5-dev libncursesw5-dev
#
# Compiler:
# C++ Standard  >= 11
#
# Notice:
# 1. Remove openssl-dev from your system, include "/usr/incude/openssl"
#
# Usage:
# build_3rdpart.sh
##############################################
#!/bin/bash

# thread count
export threadCount="10"

# compile flag
export CFLAGS="-fPIC"

# compile time
baseNow=`date '+%Y-%m-%d %H:%M:%S'`

# current directory
baseDir=$(cd `dirname $0`; pwd)

# Third libraries environment.
archName=`uname -m`
if [[ "$archName" != "aarch64" ]]; then
	archName=linux
fi
export PKTDIR=$baseDir/dependence
export DEPDIR=$baseDir/dependence/$archName/dept
export OUTDIR=$baseDir/outdir/$archName

if [ ! -d ${DEPDIR} ]; then
    mkdir ${DEPDIR} -p
fi

if [ ! -d ${OUTDIR} ]; then
    mkdir ${OUTDIR} -p
fi

echo "timestamp $baseNow, current directory is $baseDir, depdir is ${DEPDIR}, output dir is ${OUTDIR}"

# Compile third libraries source dir
#boost_1_75_0
echo "compile boost_1_75_0..."
if [ ! -f "${OUTDIR}/boost_1_75_0/lib/libboost_thread.a" ]; then
    if [ ! -d "${DEPDIR}/boost_1_75_0" ]; then
        tar -xzvf ${PKTDIR}/boost_1_75_0.tar.gz -C ${DEPDIR}
    fi
	cd ${DEPDIR}/boost_1_75_0
	chmod a+x bootstrap.sh
	chmod a+x tools/build/src/engine/build.sh
	./bootstrap.sh stage --with-libraries=all --with-toolset=gcc
	./b2 -j${threadCount} --prefix=${OUTDIR}/boost_1_75_0 variant=release include=static link=static cxxflags=-fPIC cflags=-fPIC install
else 
	echo "boost_1_75_0 libraries already exists, skip..."
fi

#openssl-1.1.1d
echo "compile openssl-1.1.1d..."
if [ ! -f "${OUTDIR}/openssl-1.1.1d/lib/libcrypto.a" ]; then
    if [ ! -d "${DEPDIR}/openssl-1.1.1d" ]; then
        tar -xzvf ${PKTDIR}/openssl-1.1.1d.tar.gz -C ${DEPDIR}
    fi
	cd ${DEPDIR}/openssl-1.1.1d
	./config --prefix=${OUTDIR}/openssl-1.1.1d -fPIC -static
	make -j ${threadCount} && make install
else 
	echo "openssl-1.1.1d libraries already exists, skip..."
fi

# spdlog-1.14.1
echo "compile spdlog-1.14.1..."
if [ ! -f "${OUTDIR}/spdlog-1.14.1/lib/libspdlog.a" ]; then
    if [ ! -d "${DEPDIR}/spdlog-1.14.1" ]; then
        tar -xzvf ${PKTDIR}/spdlog-1.14.1.tar.gz -C ${DEPDIR}/
    fi
    cd ${DEPDIR}/spdlog-1.14.1
	mkdir build -p
	cd build
	cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${OUTDIR}/spdlog-1.14.1 ..
    make -j ${threadCount} && make install
else
    echo "spdlog-1.14.1 libraries already exists, skip..."
fi

# libevent-2.1.12
echo "compile libevent-2.1.12..."
if [ ! -f "${OUTDIR}/libevent-2.1.12/lib/libevent.a" ]; then
    if [ ! -d "${DEPDIR}/libevent-2.1.12" ]; then
        tar -xzvf ${PKTDIR}/libevent-2.1.12-stable.tar.gz -C ${DEPDIR}/
    fi
    cd ${DEPDIR}/libevent-2.1.12-stable
    ./autogen.sh
    ./configure --enable-shared=no --disable-openssl --prefix=${OUTDIR}/libevent-2.1.12
    make -j ${threadCount} && make install
else
    echo "libevent-2.1.12 libraries already exists, skip..."
fi

# rapidjson-1.1.0
echo "compile rapidjson-1.1.0 begin"
if [ ! -d "${OUTDIR}/rapidjson-1.1.0/include" ]; then
    if [ ! -d "${DEPDIR}/rapidjson-1.1.0" ]; then
        tar -xzvf ${PKTDIR}/rapidjson-1.1.0.tar.gz -C ${DEPDIR}/
    fi
    cd ${DEPDIR}/rapidjson-1.1.0
    mkdir build -p
    cd build
    cmake -DCMAKE_BUILD_TYPE=Release -DRAPIDJSON_BUILD_DOC=OFF -DRAPIDJSON_BUILD_EXAMPLES=OFF -DRAPIDJSON_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=${OUTDIR}/rapidjson-1.1.0 ..
    make -j ${threadCount} && make install
else
    echo "rapidjson-1.1.0 libraries already exists, skip..."
fi

# googletest-1.15.2
echo "compile googletest-1.15.2 begin"
if [ ! -f "${OUTDIR}/googletest-1.15.2/lib/libgtest.a" ]; then
    if [ ! -d "${DEPDIR}/googletest-1.15.2" ]; then
        tar -xzvf ${PKTDIR}/googletest-1.15.2.tar.gz -C ${DEPDIR}/
    fi
    cd ${DEPDIR}/googletest-1.15.2
    mkdir build -p
    cd build
    cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${OUTDIR}/googletest-1.15.2 ..
    make -j ${threadCount} && make install
else
    echo "googletest-1.15.2 libraries already exists, skip..."
fi

